<template>
  <div>
    <el-form :rules="rules"
             ref="loginForm"
             :model="loginForm"
             class="loginContainer"

    >
      <h3 style="font-size: x-large">登录</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="false" placement="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input id="loginPassword" placeholder="请输入密码" v-model="loginForm.password" show-password></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input type="text" v-model="loginForm.code" placeholder="点击图片更换验证码" auto-complete="false" style=" width: 55% ;
    margin-left: 0px;margin-right: 5px"></el-input>
        <img :src="captchaUrl" @click="updateCaptchaUrl">
      </el-form-item>
      <!--      <el-checkbox v-model="loginForm.rememberMe" class="loginRemember">记住我</el-checkbox>-->
      <br>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark"></div>
        </el-col>
      </el-row>
      <el-form-item>
        <el-col :span="2"></el-col>
        <el-col :span="20">
          <el-button type="primary" style="width: 60%" @click="loginSubmit('loginForm')">登录</el-button>
          <el-button @click="resetBtn('loginForm')">重置</el-button>
        </el-col>
      </el-form-item>

    </el-form>
  </div>

</template>

<script>

import {delCookie, postRequest} from "../../utils/api";

export default {
  name: "login",
  comments: {},
  data() {
    return {
      captchaUrl: '/captcha?time=' + new Date(),
      loginForm: {
        username: '',
        password: '',
        code: '',
        rememberMe: true
      },
      loading: false,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 4, max: 4, message: '请输入 4 位数的验证码', trigger: 'blur'}
        ]
      },

    }
  },
  methods: {
    loginSubmit(formName) {
      this.$refs[formName].validate((value) => {
        if (value) {
          postRequest("/login", this.loginForm).then(resp => {
            // this.loading = true;
            // alert(JSON.stringify(massage))
            if (resp && resp.code != 401) {

              //储存用户信息
              window.sessionStorage.setItem('user', JSON.stringify(resp.data));

              window.sessionStorage.setItem('token', resp.data.token);

              //跳转页面
              let path = this.$route.query.redirect;
              this.$router.replace('/');
            } else {
              this.$router.push({name: 'login'});
              location.reload();
            }
          })
        } else {
          this.loading = false;
          this.$message.error("输入所有字段");
          return false;
        }
      });
    },
    resetBtn(formName) {
      this.$refs[formName].resetFields();
    },
    updateCaptchaUrl() {
      this.captchaUrl = '/captcha?time=' + new Date();
      delCookie();
    }
  },
  mounted() {

  },
}

</script>

<style>


.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: white;
  border: 1px solid azure;
  box-shadow: 0 0 25px white;
}

.loginRemember {
  margin: 0px 0px 10px -280px;
}

</style>
